package com.symbio.ishenapi.patient.controller;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.symbio.ishenapi.common.controller.BaseController;
import com.symbio.ishenapi.patient.model.*;
import com.symbio.ishenapi.patient.model.TmPatientCount;
import com.symbio.ishenapi.patient.model.TmPatientCount;
import com.symbio.ishenapi.patient.model.TmPatientCount;
import com.symbio.ishenapi.patient.model.base.BaseTmPatientCount;
import com.symbio.ishenapi.patient.model.base.BaseTmPatientCount;
import io.jboot.web.controller.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 步数
 */
@RequestMapping("/TmPatientCountController")
public class TmPatientCountController extends BaseController {

    public void saveTmPatientCount(){       //步数录入
        TmPatientCount user = (TmPatientCount)getReqBodyObject(TmPatientCount.class);
        TmPatientCount tmPatientCount = TmPatientCount.dao.findOneCount(user);
        if(tmPatientCount == null){
            user.setCreateTime(new Date());
            if (user.getCountValue() == null || "".equals(user.getCountValue())) {
                renderJsonError(40003, "步数");
            }else if (user.getUserId() == null || "".equals(user.getUserId())){
                renderJsonError(40003, "患者编号");
            }else if (user.getRuntime() == null || "".equals(user.getRuntime())){
                renderJsonError(40003, "执行时间");
            }
            else {
                user.save();
                renderJsonError(40001,"步数");
            }
        }else{
            tmPatientCount.setCountValue(user.getCountValue());
            tmPatientCount.setStatus("1");
            tmPatientCount.setCreateTime(new Date());
            tmPatientCount.setRuntime(user.getRuntime());
            if(tmPatientCount.update()){
                renderJsonError(40001,"步数");
            }else{
                renderJsonError(40005);
            }
        }

    }
    public void findTmPatientCount(){       //查看步数
        TmPatientCount user = (TmPatientCount)getReqBodyObject(TmPatientCount.class);
        //TmPatientWeight user = (TmPatientWeight) getReqBodyObject(TmPatientWeight.class);
        if(user.getUserId() == null || "".equals(user.getUserId())){
            renderJsonError(40003, "患者编号");
        }
        System.out.println(user.getUserId() + "wwwwwwwwww" + user.getId());
        List<TmPatientCount> list = TmPatientCount.dao.findTmPatientCount(user);
        List<TmPatientCount> weightList = new ArrayList<TmPatientCount>();
        Date data = null;
        List<String[]> mapList = new ArrayList<String[]>();
        String[] arrary = new String[2];
        if (list != null && list.size() > 0) {
            //算出两个时间间隔天数
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = null;
            try {//把runTime转成date
                d = sdf.parse(list.get(list.size()-1).getRuntime());
            }catch (Exception e){
                e.printStackTrace();
            }
            String da = sdf.format(new Date());

            try {//把当前时间转成"yyyy-MM-dd"
                data = sdf.parse(da);
            }catch (Exception e){
                e.printStackTrace();
            }
            int days = (int) ((data.getTime() - d.getTime()) / (1000 * 3600 * 24));
            Map<String, Integer> map = new HashMap<String, Integer>();

            Long time = d.getTime();
            for (int i = 0; i <= days; i++) {
                if (i > 0) {
                    time = time + 86400000;
                }
                //时间戳转Date
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//12小时制
                Date date = new Date();
                date.setTime(time);
                arrary = new String[2];
                arrary[0] = simpleDateFormat.format(date).toString();
                arrary[1] = "0";
                mapList.add(arrary);
            }
        }

        List<String[]> lists = new ArrayList<String[]>();
        for (int k = 0; k < mapList.size(); k++) {
            for (TmPatientCount tmPatientCount : list) {
                String[] a = mapList.get(k);
                String createtime = tmPatientCount.getRuntime();
                if (a[0].equals(createtime)) {//将查出来的数据替换到mapList里
                    a[1] = tmPatientCount.getCountValue();
                }
            }
        }
        renderJsonRet(mapList);
    }

    public void updTmPatientCount(){        //修改步数
        TmPatientCount user = (TmPatientCount) getReqBodyObject(TmPatientCount.class);
        TmPatientCount u = TmPatientCount.dao.findById(user.getId());
        if (u!=null) {
            if (!StrKit.isBlank(user.getCountValue())) u.setCountValue(user.getCountValue());
            if (!StrKit.isBlank(user.getRuntime())) u.setRuntime(user.getRuntime());
            if (u.update()) {
                renderJsonRet(user.getId());
            } else {
                renderJsonError(40005);
            }
        }else{
            renderJsonError(40003);
            return;
        }
    }

    /**
     * wenjun
     * 查询步数最新一条数据
     */
    public void findOneCount(){
        TmPatientCount user = (TmPatientCount)getReqBodyObject(TmPatientCount.class);

        if (null == user.getUserId() && "".equals(user.getUserId())) {
            renderJsonError(40005);
        }
        if(null==user.getRuntime() || "".equals(user.getRuntime())){
            Date date =new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
            String time = sdf.format(date);
            user.setRuntime(time);
        }

        TmPatientCount list=TmPatientCount.dao.findOneCount(user);

        if(null != list ){
            renderJsonRet(list);
        }else{
            list = new TmPatientCount();
            renderJsonRet(list);
        }

    }


    /**
     * 根据userId和CreateTime查询数据单一
     */
    public void findUseIdByCreateTime() {
        TmPatientCount patientCount = (TmPatientCount) getReqBodyObject(TmPatientCount.class);
        if (patientCount.getCreateTime() == null || "".equals(patientCount.getCreateTime())) {
            renderJsonError(40003, "时间");
        } else if (patientCount.getUserId() == null || "".equals(patientCount.getUserId())) {
            renderJsonError(40003, "患者编号");
        }
        System.out.println(patientCount.getUserId() + "wwwwwwwwww" + patientCount.getCreateTime());
        List<TmPatientCount> list = TmPatientCount.dao.findUserIdByCreateTime(patientCount);
        TmPatientCount tpc = new TmPatientCount();
        if (list != null && list.size() > 0) {
            tpc = list.get(0);
        }
        renderJsonRet(tpc);
    }

}
